package com.wardrobe.soa.api.store;

import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.domain.store.StoreBean;
import com.wardrobe.domain.store.StoreShowBean;
import com.wardrobe.soa.common.ApiResult;
import com.wardrobe.soa.utils.CommonConsant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/11/20.
 */
@RestController
@RequestMapping("/api/store")
public class StoreController {
    private static final Logger logger = LoggerFactory.getLogger(StoreController.class);
    @Autowired
    private StoreService storeService;

    @RequestMapping("/storeBookingList")
    public ApiResult getStoreBookingList(long userId){
        try {
            List<StoreShowBean> list = storeService.getStoreBookingList(userId);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,list);
        }catch (Exception e){
            logger.error("storeBookingList_err",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }

    @RequestMapping("/storeGoodsList")
    public ApiResult getStoreGoodsList(long userId){
        try {
            List<StoreShowBean> list = storeService.getStoreGoodsList(userId);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,list);
        }catch (Exception e){
            logger.error("storeGoodsList_err",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }

    @RequestMapping("/saveStore")
    public ApiResult saveStore(StoreBean storeBean){
        try {
            int flag = storeService.saveStore(storeBean);
            if(flag==1){
                return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,flag);
            }else{
                return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,flag);
            }
        }catch (Exception e){
            logger.error("saveStore_err",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }

    @RequestMapping("/cancelStore")
    public ApiResult cancelStore(long id ){
        try {
            //id store_id
            BackEndFromBean fromBean = new BackEndFromBean();
            fromBean.setUpdateTime(new Date());
            fromBean.setKey("status");
            fromBean.setValue("-1");
            fromBean.setId(id);
            int flag = storeService.modifyStore(fromBean);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,flag);
        }catch (Exception e){
            logger.error("saveStore_err",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }
}
